#include <bits/stdc++.h>
// 2025/04/05
// tag:
// Author: Forever
using namespace std;
using int64 = int64_t;

class UF {
public:
    int parents[101], cnt;

    UF(int n)
    {
        cnt = n;
        for (int i = 1; i <= n; i++)
            parents[i] = i;
    };

    int find(int i)
    {
        if (parents[i] != i)
            return parents[i] = find(parents[i]);
        return parents[i];
    }

    void un(int i, int j)
    {
        int ri = find(i), rj = find(j);
        if (ri == rj)
            return;
        parents[ri] = rj;
        cnt--;
    }

    int count()
    {
        return cnt;
    }

    bool connected(int p, int q)
    {
        return find(p) == find(q);
    }
};

class edge {
public:
    int u, v, w;
    edge(int u, int v, int w)
        : u(u)
        , v(v)
        , w(w) { };
    edge()
        : u(0)
        , v(0)
        , w(0) { };
    bool operator<(const edge& e) const
    {
        return w < e.w;
    }
};

int n, m, len, ans = 1;
edge edges[1001];
unordered_map<int, int> mp;
unordered_map<int, vector<edge>> mp2;
vector<int> v, tree;

int dfs(int start, int cnt){
    if (cnt == 0) {
        UF uf(n);
        for (int x: tree) 
            if (edges[x].w != len)
                uf.un(edges[x].u, edges[x].v);
        for (int x: v)
            uf.un(mp2[len][x].u, mp2[len][x].v);
        if (uf.count() == 1)
            return 1;
        else
            return 0;
    }
    int res = 0;
    for (int i = start; i < mp2[len].size() - cnt + 1; i++) {
        v.push_back(i);
        res += dfs(i + 1, cnt - 1);
        v.pop_back();
    }
    return res;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        edges[i] = edge(u, v, w);
        mp2[w].push_back(edge(u, v, w));
    }
    sort(edges + 1, edges + m + 1);
    UF uf(n);
    for (int i = 1; i <= m; i++) {
        if (uf.connected(edges[i].u, edges[i].v))
            continue;
        uf.un(edges[i].u, edges[i].v);
        mp[edges[i].w]++;
        tree.push_back(i);
    }
    if (uf.count() != 1){
        cout << 0 << endl;
        return 0;
    }
    for (auto& [k, v]: mp) {
        len = k;
        ans *= dfs(0, v), ans %= 31011;
    }
    cout << ans << endl;
    return 0;
}